package com.masterclient.app.modules.order.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;
import com.masterclient.app.base.ConfirmDialog;
import com.masterclient.app.base.MainViewHolder;

public class SubmitCustodyAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;


    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;
    private String isCustody;
    private ImageView checkCustody;
    private ConfirmDialog confirmDialog;
    private boolean firstShowCustodyDialog;

    OnCustodyClick onCustodyClick;

    public void setOnCustodyClick(OnCustodyClick onCustodyClick) {
        this.onCustodyClick = onCustodyClick;
    }

    public interface OnCustodyClick{
        void OnClick(String is_custody);
    }

    public SubmitCustodyAdapter(Activity context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, String is_custody,boolean first_show_custody_dialog) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.isCustody = TextUtils.isEmpty(is_custody)?"0":is_custody;
        this.firstShowCustodyDialog = first_show_custody_dialog;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.view_confirm_custody, parent, false));
    }
    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        if (confirmDialog == null){
            confirmDialog = new ConfirmDialog(mContext,"该订单是否需要为您开启保管？","暂不保管","确定保管","#FF6809");
            confirmDialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
                @Override
                public void cancel() {
                    confirmDialog.dismiss();
                }

                @Override
                public void confirm() {
                    isCustody = "1".equals(isCustody)?"0":"1";
                    onCustodyClick.OnClick(isCustody);
                    confirmDialog.dismiss();
                }
            });
            if (firstShowCustodyDialog) {
                confirmDialog.show();
            }else {
                firstShowCustodyDialog = true;
                isCustody = "1";
                onCustodyClick.OnClick(isCustody);
            }
        }


        checkCustody = holder.itemView.findViewById(R.id.check_custody);
        checkCustody.setImageResource("1".equals(isCustody)?R.mipmap.button_switch_select:R.mipmap.button_switch_default);
        checkCustody.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(isCustody)||"0".equals(isCustody)) {
                    confirmDialog.show();
                }else {
                    isCustody = "1".equals(isCustody)?"0":"1";
                    onCustodyClick.OnClick(isCustody);
                }
            }
        });

        holder.itemView.findViewById(R.id.tv_custody_help).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConfirmDialog helpDialog = new ConfirmDialog(mContext,"让上级保管，是指你购买的订单商品不会寄送到您这里，会让公司仓库代保管，货物一直保存在仓库，后续可以发起提货申请","","确定",1,"#FF6809");
                helpDialog.setOnChooseListerner(new ConfirmDialog.OnChooseListerner() {
                    @Override
                    public void cancel() {

                    }

                    @Override
                    public void confirm() {
                        helpDialog.dismiss();
                    }
                });
                helpDialog.show();
            }
        });

    }

    public void setIsCustody(String isCustody){
        if (checkCustody == null) return;
        this.isCustody = isCustody;
        checkCustody.setImageResource("1".equals(isCustody)?R.mipmap.button_switch_select:R.mipmap.button_switch_default);
    }


    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 9;
    }
}
